package com.ioudi.digu.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.ioudi.digu.annotation.TypeDefination;
import com.ioudi.digu.dao.MessagerPossessDAO;
import com.ioudi.digu.service.IGeneralService;
import com.ioudi.digu.service.IMessagerPossessService;
import com.ioudi.digu.service.UserAwareService;
import com.ioudi.digu.service.drm.DatasourceUserAwareService;
import com.ioudi.digu.service.support.ListResult;
import com.ioudi.digu.util.IDUtil;
import com.ioudi.digu.vo.Messager;
import com.ioudi.digu.vo.MessagerPossess;

@WebService (
		  endpointInterface = "com.ioudi.digu.service.IMessagerPossessService"
		)
public class MessagerPossessService extends DatasourceUserAwareService implements IMessagerPossessService
{

	private MessagerPossessDAO possessDAO;
	
	private IGeneralService messagerService;
	
    public IGeneralService getMessagerService()
	{
		return messagerService;
	}

	public void setMessagerService(IGeneralService messagerService)
	{
		this.messagerService = messagerService;
	}

	private String defaultMessagerId;


	public String getDefaultMessagerId()
	{
		return defaultMessagerId;
	}

	public void setDefaultMessagerId(String defaultMessagerId)
	{
		this.defaultMessagerId = defaultMessagerId;
	}


	public MessagerPossessDAO getPossessDAO()
	{
		return possessDAO;
	}

	public void setPossessDAO(MessagerPossessDAO possessDAO)
	{
		this.possessDAO = possessDAO;
	}
	
	@TypeDefination(type=MessagerPossess.class)
	public Object createItem(@TypeDefination(type=Map.class, fieldNames = {"user", "messagerId", "payfored"})
			Object item)
	{
		if(item instanceof Map)
		{
			Map map = (Map)item;
			return createPossess(String.valueOf(map.get("user")), String.valueOf(map.get("messagerId")), 
					(Boolean)map.get("payFored"));
		}
		return null;
	}
	
	private MessagerPossess createPossess(String user, String messagerId,
			boolean payFored)
	{
		Object raw = messagerService.getItem(messagerId);
		Messager messager = raw instanceof Messager ? (Messager)raw : null;
		if(messager == null)
		{
			return null;
		}
		MessagerPossess possess = new MessagerPossess();
		possess.setId(IDUtil.createID(user, "messagerPossess"));
		possess.setUser(user);
		possess.setMessager(messager);
		possess.setLastUse(new Date());
		possess.setPayFored(payFored);
		boolean result = possessDAO.saveOrUpdateMessager(possess);
		return result ? possess : null;
	}
	
	public boolean deleteItem(String[] ids)
	{
		return possessDAO.deleteMessagerPossess(ids);
	}

	public Object getItem(String id)
	{
		return null;
	}

	public List getItems(int index, int maxCount, Map condition)
	{
		if(condition == null && index == 0 && getUser() != null && isEmpty())
		{
			createDefaultMessagerPossess(getUser());
		}
		return possessDAO.findPossessNoTotal(index, maxCount, condition);
	}

	public ListResult getItemsWithTotal(int index, int maxCount, Map condition)
	{
		if(condition == null && index == 0 && getUser() != null && isEmpty())
		{
			createDefaultMessagerPossess(getUser());
		}
	    return possessDAO.findPossess(index, maxCount, condition);
	}
	
	private boolean isEmpty()
	{
		List list =  possessDAO.findPossessNoTotal(0, -1, null);
		return list == null || list.size() <= 0;
	}
	
	public MessagerPossess createDefaultMessagerPossess(String user)
	{
		return  createPossess(user, this.defaultMessagerId, true);
	}

	public Object updateItem(String id, Map updateMap)
	{
		return null;
	}
	
	public String getRenderer(String sender, String contentType,
			boolean applyUser)
	{
		if(contentType == null || contentType.length() <= 0)
		{
			contentType = "text";
		}
		String url = null;
		String user = null;
		if(applyUser)
		{
			user = getUser();
		}
		MessagerPossess mp = getPossess(user, contentType, true);
		if(mp == null)
		{
			mp = getPossess(sender, contentType, true);
		}
		if(mp == null)
		{
			mp = getPossess(null, contentType, true);
		}
		return mp == null ? null : mp.getMessager().getRendererURL();
	}
	
	private MessagerPossess getPossess(String user, String contentType, boolean render)
	{
		String old = getDatasourceDistribute().currentDatasource();
		Map condition = new HashMap();
		if(user != null)
		{
			condition.put("user", user);
			getDatasourceDistribute().lookupDatasource(user, true);
		}	
		condition.put("contentType", contentType);
		condition.put("rendererURL", render);
		List list = getItems(0, 1, condition);
		getDatasourceDistribute().useDatasouce(old);
		return list != null ? (MessagerPossess)list.get(0) : null;
	}

}
